package com.example.myrecipes;

import java.lang.reflect.Type;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MainActivity extends ListActivity {
	public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
	public final static String FILENAME = "my_recipe";
	ArrayList<RecipeBox> recipeCollections;
	ArrayAdapter<RecipeBox> arrayAdapter;

	EditText editText;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		SharedPreferences preferences = getPreferences(MODE_PRIVATE);

		String JsonString = preferences.getString("RecipeBox", null);

		Gson gson = new Gson();

		if(JsonString != null) {
			Type listType = new TypeToken<ArrayList<RecipeBox>>(){}.getType();
			recipeCollections = gson.fromJson(JsonString, listType);
		} else {
			recipeCollections = new ArrayList<RecipeBox>();
		}


		arrayAdapter = new ArrayAdapter<RecipeBox>(this, android.R.layout.simple_list_item_1,recipeCollections);
		setListAdapter(arrayAdapter);
		/*GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new ImageAdapter(this));

		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
			}
		});*/
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void addRecipeCollection(View view) {
		// Do something in response to button click
		editText = (EditText) findViewById(R.id.edit_recipe_collection);
		String recipeCollectionName = editText.getText().toString();
		RecipeBox recipeBox = new RecipeBox(recipeCollectionName);
		this.arrayAdapter.add(recipeBox);
		this.editText.setText("");

	}

	public void clearPreferences(View view) {
		SharedPreferences preferences = getPreferences(MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();

		editor.clear();
		editor.commit();
	}

	public void onListItemClick(ListView l, View v, int position, long id) {
		RecipeBox selection = (RecipeBox) l.getItemAtPosition(position);
		Intent intent = new Intent(this, RecipeBoxActivity.class);
		intent.putExtra("RecipeBox", selection);
		startActivity(intent);
	}
	protected void onPause() {
		super.onPause();

		SharedPreferences preferences = getPreferences(MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		Gson gson = new Gson();
		//JsonArray jsArray = new JsonArray();
		String result = gson.toJson(recipeCollections);
		//System.out.println( "TAG " + result);
		editor.putString("RecipeBox", result);
		editor.commit();
	}
}
